/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session07;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author macbookpro
 */
public class ClassInfo {

    public static void main(String[] args) {
        List l1 = new ArrayList();
        List l2 = new Vector();
        printClassName(l1);
        printClassName(l2);

        System.out.println("\nMethods:");
        printMethods(l1);

        
        Object obj = getInstance(l2);
        if(obj instanceof List){
            List l3 = (List)obj;
            l3.add("abc");
            l3.add("def");
            System.out.println("l3: " + l3);
        }
        
        System.out.println("l1: " + l1);
        System.out.println("l2: " + l2);
    }

    public static void printClassName(Object obj) {
        Class c = obj.getClass();
        System.out.println(c.getName());
    }

    public static void printFields(Object obj) {
        Class c = obj.getClass();
    }

    public static void printMethods(Object obj) {
        Class c = obj.getClass();

        Method ms[] = c.getMethods();

        String str = "";

        for (Method m : ms) {
            Class cl[] = m.getParameterTypes();
            str = "";
            for (Class c1 : cl) {
                if (str.equals("")) {
                    str = c1.getName();
                } else {
                    str += ", " + c1.getName();
                }
            }
            System.out.println(m.getReturnType() + " " + m.getName() + "(" + str + ")");
        }
    }

    public static Object getInstance(Object o) {
        Class c = o.getClass();
        try {
            return c.newInstance();
        } catch (InstantiationException ex) {
            Logger.getLogger(ClassInfo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ClassInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
